#include <stdio.h>

float print_odd(int);
float print_even(int);

/**
 * 题目：编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用函数指针)。
*/
int main(int argc, char const *argv[])
{
    int n;
    float result;
    float (*pfunc)(int);
    printf("请输入数字：");
    scanf("%d", &n);
    if(n%2==0)
        pfunc = print_even;
    else
        pfunc = print_odd;
    
    result = (*pfunc)(n);
    printf("结果为：%f\n", result);

    return 0;
}

float print_odd(int n)
{
    float sum = 0;
    float item = 0;
    for(int i = 1; i <= n; i=i+2)
    {
        item = (float)1 / i;
        sum += item;
    }
    return sum;
}

float print_even(int n)
{
    float sum = 0;
    float item = 0;
    for(int i = 2; i <= n; i=i+2)
    {
        item = (float)1 / i;
        sum += item;
    }
    return sum;
}